package com.usv.comparepic;

import static java.lang.Math.abs;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.usv.drawer.DifferenceDrawer;
import com.usv.drawer.impl.DifferenceFileDrawer;

public class ComparePic {

    public static Color COLOR_DIFF = Color.red;
    public static int PERCENT_TOLERANCE = 10;
    private DifferenceDrawer diffDrawer = new DifferenceFileDrawer();

    public boolean compareTwoImages(BufferedImage bufImgOne, BufferedImage bufImgTwo) {

        if (isDifferentSize(bufImgOne, bufImgTwo)) {
            System.out.println("Pictures are of different sizes.");
            return false;
        }

        List<Point> diffPoints = new ArrayList<>();
        int imgTwoHt = bufImgTwo.getHeight();
        int imgTwoWt = bufImgTwo.getWidth();
        for (int x = 0; x < imgTwoWt; x++) {
            for (int y = 0; y < imgTwoHt; y++) {
                Point point = new Point(x, y);
                if (isDifferentPixel(bufImgOne, bufImgTwo, point, PERCENT_TOLERANCE)) {
                    diffPoints.add(point);
                }
            }
        }

        if (diffPoints.isEmpty()) {
            System.out.println("Both of the pictures look the same.");
            return true;
        }

        findSelections(diffPoints, bufImgTwo);
        return false;

    }

    private static boolean isDifferentSize(BufferedImage bufImgOne, BufferedImage bufImgTwo) {

        int imgOneHt = bufImgOne.getHeight();
        int imgTwoHt = bufImgTwo.getHeight();
        int imgOneWt = bufImgOne.getWidth();
        int imgTwoWt = bufImgTwo.getWidth();
        return imgOneHt != imgTwoHt || imgOneWt != imgTwoWt;
    }

    private boolean isDifferentPixel(BufferedImage bufImgOne, BufferedImage bufImgTwo,
            Point point, int percent) {
        int x = point.getX();
        int y = point.getY();

        boolean result = false;
        int tolerance = 255 * percent / 100;

        int pixel1 = bufImgOne.getRGB(x, y);
        int pixel2 = bufImgTwo.getRGB(x, y);

        int alpha1 = (pixel1 >> 24) & 0xff;
        int red1 = (pixel1 >> 16) & 0xff;
        int green1 = (pixel1 >> 8) & 0xff;
        int blue1 = (pixel1 >> 0) & 0xff;

        int alpha2 = (pixel2 >> 24) & 0xff;
        int red2 = (pixel2 >> 16) & 0xff;
        int green2 = (pixel2 >> 8) & 0xff;
        int blue2 = (pixel2 >> 0) & 0xff;

        if (abs(alpha1 - alpha2) > tolerance || abs((red1 - red2)) > tolerance
                || abs(green1 - green2) > tolerance || abs(blue1 - blue2) > tolerance) {

            result = true;
        }
        return result;
    }

    private void findSelections(List<Point> diffPoints, BufferedImage bufImgTwo) {
        Point startPointX = Collections.min(diffPoints, new ComparatorPointX());
        
         Point endPointX = Collections.max(diffPoints, new ComparatorPointX());
      
         Point startPointY = Collections.min(diffPoints, new ComparatorPointY());
         Point endPointY = Collections.max(diffPoints, new ComparatorPointY());
         Point startPoint = new Point (startPointX.getX(), startPointY.getY());
         Point endPoint = new Point (endPointX.getX(), endPointY.getY());
         System.out.println(startPointX);
            System.out.println(endPointX);
              System.out.println(startPointY);
            System.out.println(endPointY);
//        int endIndex = 0;
//        int x1 = 0;
//        int y1 = 0;
//        int x2 = 0;
//        int y2 = 0;
//
//        while (startIndex < diffPoints.size() - 1) {
//
//            x1 = listX.get(startIndex);
//            y1 = listY.get(startIndex);
//            x2 = x1;
//            y2 = y1;
//
//            while (listX.contains(x2)) {
//                endIndex = listX.lastIndexOf(x2);
//                x2++;
//            }
//
//            y1 = Collections.min(listY.subList(startIndex, endIndex));
//            y2 = Collections.max(listY.subList(startIndex, endIndex));

            diffDrawer.drawDifference(bufImgTwo, startPoint, endPoint, COLOR_DIFF);

//            startIndex = endIndex + 1;
//        }
    }
}
